package com.sicheng.lc.周赛.分类.模拟.矩阵行走;

import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/6 12:11
 */
public class 执行所有后缀指令 {
    //https://leetcode.cn/problems/execution-of-all-suffix-instructions-staying-in-a-grid/
    public int[] executeInstructions(int n, int[] startPos, String s) {
        int i = startPos[0];
        int j = startPos[1];
        int[] res = new int[s.length()];
        for (int k = 0; k < s.length(); k++) {
            int x = i, y = j;
            int r = k;
            while (r < s.length()) {
                if (s.charAt(r) == 'R')
                    y++;
                else if (s.charAt(r) == 'L')
                    y--;
                else if (s.charAt(r) == 'D')
                    x++;
                else
                    x--;
                if (x < 0 || x >= n || y < 0 || y >= n) {
                    break;
                }
                r++;
            }
            res[k] = r - k ;
        }
        return res;
    }

    public static void main(String[] args) {

        执行所有后缀指令 s = new 执行所有后缀指令();
        System.out.println(Arrays.toString(s.executeInstructions(3, new int[]{0, 1}, "RRDDLU")));
    }
}
